package com.example.gkcircle.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import com.example.gkcircle.BaseActivity;
import com.example.gkcircle.R;
import com.example.gkcircle.utils.CommonUtil;
import com.example.gkcircle.utils.MyAlertView;
import com.example.gkcircle.utils.StringUtil;
import com.example.gkcircle.widget.EditTextWithDelete;

/**
 * 注册页
 * Created by Jehu on 16/3/25.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener{


    private EditTextWithDelete mPhoneEdit;
    private EditTextWithDelete mCaptchaEdit;
    private Button mGetCaptchaBtn;
    private ImageButton mCloseBtn;
    private TextView mNextTv;
    private TextView mTitle;
    private final int RIGISTER_FLAG=0;
    private final int RESET_PASSWORD_FLAG=1;
    private String mPhone;


    private boolean isAbleResendSMS=true;
    @Override
    protected void initView() {
        setContentView(R.layout.register_activity);
        this.mPhoneEdit=(EditTextWithDelete)this.findViewById(R.id.register_phone_edit);
        this.mCaptchaEdit=(EditTextWithDelete)this.findViewById(R.id.register_captcha_edit);
        this.mGetCaptchaBtn=(Button)this.findViewById(R.id.register_get_captcha_btn);
        this.mCloseBtn=(ImageButton)this.findViewById(R.id.register_close_btn);
        this.mNextTv=(TextView)this.findViewById(R.id.register_next_tv);
        this.mTitle=(TextView)this.findViewById(R.id.register_title);
    }

    @Override
    protected void initListener() {
        mGetCaptchaBtn.setOnClickListener(this);
        mCloseBtn.setOnClickListener(this);
        mNextTv.setOnClickListener(this);
    }

    @Override
    protected void setData() {
        switch (getIntent().getExtras().getInt("flag")){
            //注册
            case RIGISTER_FLAG:
                mTitle.setText(getResources().getString(R.string.phone_register));
                break;
            //修改密码
            case RESET_PASSWORD_FLAG:
                mTitle.setText(getResources().getString(R.string.forget_psw));
                break;
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register_get_captcha_btn:
                if(!isAbleResendSMS){
                    return;
                }
                mPhone=mPhoneEdit.getText().toString().trim();
                if(mPhone.isEmpty())return;
                if(!StringUtil.isMobile(mPhone)){
                    MyAlertView.showAlertViewWithOK(RegisterActivity.this,"发送失败","手机格式错误","好");
                    return;
                }
                CommonUtil.hideSoftInputView(this);
                MyAlertView.showAlertViewWithOKAndCancel(this, "我的将向 +86 " + mPhone + " 发送验证码", new MyAlertView.IShowAlertViewWithOkAndCancelListener() {
                    @Override
                    public void onclickOk() {
                        mCaptchaEdit.setFocusable(true);
                        mCaptchaEdit.setFocusableInTouchMode(true);
                        mCaptchaEdit.requestFocus();
                        CommonUtil.hideSoftInputView(RegisterActivity.this);
                        isAbleResendSMS=false;
                        getCaptcha(mPhone);
                    }
                });
                break;
            case R.id.register_close_btn:
                finish();
                break;
            case R.id.register_next_tv:
                String code=mCaptchaEdit.getText().toString().trim();
                verifyCodeAndfinish(code);
                break;

        }
    }


    //获取验证码
    private void getCaptcha(String phone){
        mGetCaptchaBtn.setTextColor(getResources().getColor(R.color.date_grey));
        mGetCaptchaBtn.setBackground(getResources().getDrawable(R.drawable.grey_border_bg));
        mGetCaptchaBtn.setEnabled(false);
        new MyCountTimer(1000 * 60, 1000 * 1).start();
    }


    //检验验证码 进行下一步
    public void verifyCodeAndfinish(String code){
        Intent intent =new Intent(this,ResetPasswordActivity.class);
        Bundle bundle=new Bundle();
        bundle.putInt("flag",1);
        bundle.putString("phone",mPhone);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    }

    public class MyCountTimer extends CountDownTimer {

        public MyCountTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            mGetCaptchaBtn.setText("重新发送" + "(" + millisUntilFinished / 1000 + "s)");
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            mGetCaptchaBtn.setText("重新发送");
            mGetCaptchaBtn.setTextColor(getResources().getColor(R.color.white));
            mGetCaptchaBtn.setBackground(getResources().getDrawable(R.drawable.blue_to_light_selector));
            mGetCaptchaBtn.setEnabled(true);
            isAbleResendSMS = true;
        }
    }

}
